import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class demo1 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            // 加载MySQL的JDBC驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 建立与数据库的连接
            String url = "jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC";
            String user = "root";
            String password = "yuhuachengdie";
            conn = DriverManager.getConnection(url, user, password);

            // 创建Statement对象
            stmt = conn.createStatement();

            // 执行SQL查询
            String sql = "SELECT * FROM jdbc";
            rs = stmt.executeQuery(sql);

            // 处理查询结果
            while (rs.next()) {
                System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}